package com.hjm.project.formalPos.formalPos.controller;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.formalPos.formalPos.domain.CardFeePos;
import com.hjm.project.formalPos.formalPos.service.IFormalPosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * POS终端信息Controller
 *
 * @author LOVE
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/terminal/formalPos")
public class CardFeePosController extends BaseController {

    @Resource
    private IFormalPosService formalPosService;


    /**
     * 查询流量卡终端信息
     */
    // @RequiresPermissions("terminal:formalPos:list")
    @PostMapping("/cardFeeList")
    @ResponseBody
    public TableDataInfo cardFeeList(CardFeePos cardFeePos)
    throws Exception{
        if (ShiroUtils.isSale()) {
            cardFeePos.setSaleAgentNum(ShiroUtils.getLoginAgentNum());
        }
        if (ShiroUtils.isAgent()) {
            cardFeePos.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        startPage();
        List<CardFeePos> list = formalPosService.selectCardFeePos(cardFeePos);
        for(CardFeePos cf :list){
            if(cf.getTopLevelAgent() != null && !cf.getTopLevelAgent().isEmpty()){
                cf.setTopLevelAgent(cf.getTopLevelAgent().split("-")[0]);
            }
            if(cf.getTopLevelAgentName() != null && !cf.getTopLevelAgentName().isEmpty()){
                cf.setTopLevelAgentName(cf.getTopLevelAgentName().split("-")[1]);
            }
            if(cf.getTax() != null && !cf.getTax().equals("0")) {
                cf.setTax(String.valueOf(Double.parseDouble(cf.getTax()) * 100));
            }else{
                cf.setTax("0");
            }
            if (StringUtils.isNotEmpty(cf.getActivateDate())){
                String day = DateUtils.getDay("yyyy-MM-dd", 31, cf.getActivateDate());
                cf.setReturnDate(day);
            } else {
                cf.setReturnDate("");
            }
        }
        return getDataTable(list);
    }

    @RequiresPermissions("terminal:formalPos:cardFeePos")
    @GetMapping("/cardFeePos")
    public String cardFeePos()
    {
        return  "/terminal/cardFeePos/cardFeePos";
    }

    /**
     * 导出所有流量卡终端信息
     */
    //@RequiresPermissions("terminal:formalPos:ybExport")
    @Log(title = "导出所有流量卡终端信息", businessType = BusinessType.EXPORT)
    @PostMapping("/cardExport")
    @ResponseBody
    public AjaxResult cardExport(CardFeePos cardFeePos)
    throws Exception{
        List<CardFeePos> list = formalPosService.selectCardFeePos(cardFeePos);
        for (CardFeePos feePos : list) {
            if (StringUtils.isNotEmpty(feePos.getActivateDate())){
                String day = DateUtils.getDay("yyyy-MM-dd", 31, feePos.getActivateDate());
                feePos.setReturnDate(day);
            } else {
                feePos.setReturnDate("");
            }
        }
        ExcelUtil<CardFeePos> util = new ExcelUtil<CardFeePos>(CardFeePos.class);
        return util.exportExcel(list, "导出所有流量卡终端信息");
    }
}
